<table width="100%" border="1" rules="all" cellspacing="0" class="tblSort" cellpadding="0" id="tbl_qry_fac">
<thead>
  <tr>
    <th>Factura</th>
    <th>Folio</th>
    <th>Local</th>
    <th>Cliente</th>
    <th>Creador el </th>
    <th>Creado por</th>
    <th>Fe.Factura</th>
    <th>Status</th>
    <th>Monto(inc.IGV)</th>
    <th>Condicion</th>
  </tr>
  </thead>
  <tbody>
  <?php 
  include('../../Connections/KRAPPERCON.php');
  $cvta_id = '';
  $cvta_txt = '';;
  $sql= "call sp_getCondVta();";
  $res = $mysqli->query($sql);
  if ($res){
  	while ($row = $res->fetch_assoc()){
	$cvta_id[] = $row['CondVenID'];
	$cvta_txt[] = $row['CondVenTxt'];
	}
  $res->close();
  $mysqli->check_result();
  }
  
  $cldoc = $_POST['cb_lf_cldoc'];
  $fpini = $_POST['t_lf_pini'];
  $fpfin = $_POST['t_lf_pfin'];
  
  $chk_anul = getVal('chk_anul');
  
  $ffini = $_POST['t_lf_cini'];
  $fffin = $_POST['t_lf_cfin'];
  
  $cli = $_POST['t_lf_cli'];
  
  $ref = $_POST['t_lf_ref'];
  
  $local = getVal('cb_local');
  
$wh_fc_ini = '';
$wh_fc_fin = '';
$wh_ff_ini = '';
$wh_ff_fin = '';
  
$wh_cldoc = isNull($cldoc,"and ClaDocID = '".$cldoc."'");

if (isEmpty($fpfin)){
$wh_fc_ini = isNull($fpini," and date(FactFecCre) = '".$fpini."'");
}
else{
$wh_fc_ini = isNull($fpini," and date(FactFecCre) >= '".$fpini."'");
$wh_fc_fin = isNull($fpfin," and date(FactFecCre) <= '".$fpfin."'");
}

if (isEmpty($fffin)){
$wh_ff_ini = isNull($ffini," and FactFecFac = '".$ffini."'");
}
else{
$wh_ff_ini = isNull($ffini," and FactFecFac >= '".$ffini."'");
$wh_ff_fin = isNull($fffin," and FactFecFac <= '".$fffin."'");
}

$wh_loc = isNull($local," and a.CentroID = ".$local);

$wh_cli = isNull($cli," and a.ClientID = '".$cli."'");
$wh_ref = isNull($ref," and FactFolio = '".$ref."'");



$where = $wh_cldoc.$wh_cli.$wh_ref.$wh_fc_ini.$wh_fc_fin.$wh_ff_ini.$wh_ff_fin.$wh_loc;
	if (trim($where)!= '' ){
		$where = 'where '.substr(ltrim($where),3);
	}
  
  $sql = "select a.FacturaID,a.CLientID,a.FactFecCre,a.FactFecFac,a.UserID,a.FactAnul,
a.FactFolio,b.ClientNom,ClaDocID,CondVenID,FactNet,FactIva,a.CentroID from tt_facturavta as a inner join tt_cliente as b
on a.ClientID = b.clientID " .$where;
doEcho ($sql);
$res = $mysqli->query($sql);
$cond  = '';
$totales = 0;
$neto = 0;

if ($res){
	
	if ($chk_anul == 0){
		while($row = $res->fetch_assoc()){
	$cond = $cvta_txt[array_search($row['CondVenID'],$cvta_id)];
	$neto = $row['FactNet'] + $row['FactIva'];
	switch ($row['FactAnul']){
	case 0 : $status = 'Activo' ; break;
	case 1 : $status = 'Anulado' ; 
	$neto = 0;
	break;
	case 2 : $status = 'Anulacion' ;	break;
	default : $status = 'No Definido'; break;
	}
	if ($row['FactAnul']==2)
	continue;
	
	$totales = $totales + $neto;
	?>
  <tr>
    <td><a class='vfc' href="#" onclick="goFactura(this)"><?php echo $row['FacturaID']; ?></a></td>
    <td><?php echo $row['FactFolio']; ?></td>
    <td><?php echo $row['CentroID'];?></td>
    <td><?php echo $row['ClientNom']; ?></td>
    <td><?php echo $row['FactFecCre']; ?></td>
    <td><?php echo $row['UserID']; ?></td>
    <td><?php echo $row['FactFecFac']; ?></td>
    <td><?php echo $status; ?></td>
    <td class="qt"><?php echo myRound($neto); ?></td>
    <td><?php echo $cond; ?></td>
  </tr>    
    <?php 
	}
	}
	else {
		while($row = $res->fetch_assoc()){
	$cond = $cvta_txt[array_search($row['CondVenID'],$cvta_id)];
	$neto = $row['FactNet'] + $row['FactIva'];
	switch ($row['FactAnul']){
	case 0 : $status = 'Activo' ; break;
	case 1 : $status = 'Anulado' ; 
	break;
	case 2 : $status = 'Anulacion' ;	break;
	default : $status = 'No Definido'; break;
	}
	
	$totales = $totales + $neto;
	?>
  <tr>
    <td><a class='vfc' href="#" onclick="goFactura(this)"><?php echo $row['FacturaID']; ?></a></td>
    <td><?php echo $row['FactFolio']; ?></td>
    <td><?php echo $row['CentroID'];?></td>
    <td><?php echo $row['ClientNom']; ?></td>
    <td><?php echo $row['FactFecCre']; ?></td>
    <td><?php echo $row['UserID']; ?></td>
    <td><?php echo $row['FactFecFac']; ?></td>
    <td><?php echo $status; ?></td>
    <td class="qt"><?php echo myRound($neto); ?></td>
    <td><?php echo $cond; ?></td>
  </tr>    
    <?php 
	}
	}	

	
	
$res->close();
$mysqli->check_result();
}
  ?>
</tbody>
<tfoot>
  <tr>
    <th>&nbsp;</th>
    <th>&nbsp;</th>
    <th>&nbsp;</th>
    <th>&nbsp;</th>
    <th>&nbsp;</th>
    <th>&nbsp;</th>
    <th>&nbsp;</th>
    <th>&nbsp;</th>
    <th class="qt"><?php echo myRound($totales); ?></th>
    <th>&nbsp;</th>
  </tr>
</table>
<script type="text/javascript">
function goFactura(obj){
var fact = $(obj).html();
loadPopup({url : 'Procesos/verFactura.php', params : { oper : 'view', t_fc_nrodoc : fact }});
}

//$('#tbl_qry_fac tbody ').bind('contextmenu', function(e) {
	// evito que se ejecute el evento
        //alert('hola');
	//e.preventDefault();
	// conjunto de acciones a realizar
//});
$("#tbl_qry_fac tbody tr .vfc").each(function(){
    $(this).attr('href','#Procesos/verFactura.php?oper=view&t_fc_nrodoc='+this.innerHTML);
});
</script>
<?php /*
include('../../Connections/KRAPPERCON.php');
$oper = '';
$cdoc = $_POST['cb_lf_cdoc'];
$cvta = $_POST['cb_lf_cvta'];
$pini = $_POST['t_lf_pini'];
$pfin = $_POST['t_lf_pfin'];
$cini = $_POST['t_lf_cini'];
$cfin = $_POST['t_lf_cfin'];
$tcli = $_POST['t_lf_cli'];
$tref =	 $_POST['t_lf_ref'];

//tabulate_resultset($mysqli->query($sql));
*/
?>